package com.shop2cn.iapi.sqoptrade.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetCurrencyConfigResp;
import com.shop2cn.iapi.sqoptrade.parameter.enums.PayStatusEnum;
import com.shop2cn.iapi.sqoptrade.parameter.enums.PayTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.ConfirmOfflinePayReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ConfirmOfflinePayCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.shenqi.DGSQBaseService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingBookingOrderCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.ext.model.PickUpCodePo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sharding.model.PaymentPo;
import com.ymttest.database.sharding.db.sharding.model.PickInStorePo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

public class Ts_SqConfirmOfflinePay {

	private static ConfirmOfflinePayReq confirmOfflinePayReq;
	private static ConfirmOfflinePayCall confirmOfflinePayCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手确认收款");
	}

	@Before
	public void caseUp() {
		confirmOfflinePayReq = new ConfirmOfflinePayReq();
		confirmOfflinePayCall = new ConfirmOfflinePayCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmOfflinePayCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 支付完成后（包括第三方支付回调交易完成）,校验订单数据
	 * 
	 * @param paymentId
	 * @param useAccountAmount
	 * @param orderList
	 * @param cmbDiscountAmount
	 * @param placeEngageReq
	 * @throws InterruptedException 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private void verifyDb(long userId, long tradingId, BigDecimal useAccountAmount,
			List<Order> orderList) throws InterruptedException, ClientProtocolException, IOException {

		BigDecimal payDeductAmount = BigDecimal.ZERO;

		BigDecimal orderDiscount = BigDecimal.ZERO;
		BigDecimal realNeedPayAmount = BigDecimal.ZERO;
		BigDecimal freight = BigDecimal.ZERO;
		BigDecimal totalYmtCouponAmount = BigDecimal.ZERO;
		BigDecimal totalSellerCouponAmount = BigDecimal.ZERO;
		BigDecimal totalCardAmount = BigDecimal.ZERO;
		BigDecimal needPayAmount = BigDecimal.ZERO;
		BigDecimal totalTaxFee = BigDecimal.ZERO;
		BigDecimal totalRebateAmount = BigDecimal.ZERO;
		
		String payCurrency = null;
		String payment_payCurrency = null;
		String settleRate = null;
		
		long paymentId = 0L;
		
		BigDecimal[] realNeedPayAmountList = new BigDecimal[orderList.size()];
		
		totalYmtCouponAmount = orderList.stream()
				.map(o -> o.getOrderPo().getYmtCouponAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		totalSellerCouponAmount = orderList.stream()
				.map(o -> o.getOrderPo().getSellerCouponAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		totalCardAmount = orderList.stream()
				.map(o -> o.getOrderPo().getCardAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		totalTaxFee = orderList.stream()
				.map(o -> o.getOrderPo().getTaxFee())
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		totalRebateAmount = orderList.stream()
				.map(o -> o.getOrderPo().getRebatePaidAmount())
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		for (int i = 0; i < orderList.size(); i++) {

			orderShardingWapper.clearCache();
			
			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(
					orderList.get(i).getOrderId());
			orderDiscount = orderDiscount.add(orderPo.getOrderDiscount());
			payCurrency = orderPo.getCurrency();
			freight = freight.add(orderPo.getFreight());
			realNeedPayAmountList[i] = orderPo.getPayableAmount();
			realNeedPayAmount = realNeedPayAmount.add(orderPo
					.getPayableAmount());
			
			settleRate = orderPo.getExchangeRate();
			
			paymentId = orderPo.getPaymentId();
		}

		// 该交易需要支付的总金额
		needPayAmount = orderList.stream()
				.map(o -> o.getOrderPo().getTotalPrice())
				.reduce(BigDecimal.ZERO, BigDecimal::add)
				.subtract(payDeductAmount).add(orderDiscount).add(freight).add(totalTaxFee)
				.subtract(totalSellerCouponAmount)
				.subtract(totalYmtCouponAmount)
				.subtract(totalCardAmount)
				.subtract(totalRebateAmount);
		
		// 用户支付的余额
		useAccountAmount = Utils.zeroIfNull(useAccountAmount);

		// 支付表(payment)
		PaymentPo paymentPo = orderShardingWapper.selectPaymentById(paymentId);

		PaymentPo expectedPayment = new PaymentPo();

		expectedPayment.setUserId(userId);
		expectedPayment.setPaymentId(paymentId);
		expectedPayment.setAccountAmount(BigDecimal.ZERO);
		expectedPayment.setPayableAmount(needPayAmount);
		expectedPayment.setThirdpartyAmount(useAccountAmount);

		expectedPayment.setTradeType(1);
		expectedPayment.setPayStatus(PayStatusEnum.MINUS_BALANCE_SUCCESS
				.getCode());
		expectedPayment.setPayType(PayTypeEnum.OFFLINE_PAY.getCode());

		expectedPayment.setThirdpartyDiscount(BigDecimal.ZERO);
		expectedPayment.setPayDeductAmount(payDeductAmount);

		expectedPayment.setRealPaidAmount(realNeedPayAmount);
		expectedPayment.setRetryTimes(0);
		
		//如果订单上面的currency为null，则payment上面的payCurrency为CNY
		if (null == payCurrency)
			payment_payCurrency = "CNY";
		else
			payment_payCurrency = payCurrency;
		
		expectedPayment.setPayCurrency(payment_payCurrency);

		expectedPayment.setAppName(null);
		
		GetCurrencyConfigResp getCurrencyConfigResp = DGSQBaseService.getCurrencyConfig(payment_payCurrency);
		
		BigDecimal orderRmb = useAccountAmount.multiply(
				new BigDecimal(payment_payCurrency.equalsIgnoreCase("CNY") == true ? "1"
						: settleRate)).setScale(2, RoundingMode.HALF_DOWN);
		
		if (!payment_payCurrency.equalsIgnoreCase("CNY"))
			expectedPayment.setExchangeRate(new BigDecimal(settleRate)
					.setScale(5, RoundingMode.HALF_UP).toPlainString());
		else
			expectedPayment.setExchangeRate("1.00000");
		
		expectedPayment.setForeignAmount(needPayAmount);
		BigDecimal realForeignAmount = SqTradingCallService
				.calRealForeignAmount(expectedPayment.getRealPaidAmount(),
						expectedPayment.getPayableAmount(),
						expectedPayment.getForeignAmount());

		expectedPayment.setSettleCurrency(null);
		expectedPayment.setSettleRate(null);
		expectedPayment.setRealForeignAmount(realForeignAmount);
		
		expectedPayment.setShenqiPaymentRmbAmount(orderRmb);
		
		expectedPayment.setGatewayPayType(null);

		verifyPaymentDb(expectedPayment, paymentPo);

		BigDecimal[] weights = new BigDecimal[orderList.size()];

		for (int i = 0; i < orderList.size(); i++) {
			weights[i] = realNeedPayAmountList[i];
		}

		for (int i = 0; i < orderList.size(); i++) {

			// 订单号
			long orderId = orderList.get(i).getOrderId();

			// 订单表(order)
			OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

			Logger.comment("验证订单号:" + orderId);

			Logger.comment("验证订单表(order)表");

			Logger.verifyEquals(17, ymtOrders.getOrderStatus(), "验证数据库OrderStatus");
			Logger.verifyEquals(expectedPayment.getShenqiPaymentRmbAmount(),
					ymtOrders.getShenqiPaymentRmbAmount(),
					"验证数据库ShenqiPaymentRmbAmount");
			Logger.verifyEquals(getCurrencyConfigResp.getMark(),
					ymtOrders.getCurrencySymbol(),
					"验证数据库CurrencySymbol");
			Logger.verifyEquals(expectedPayment.getPayCurrency(),
					ymtOrders.getCurrency(),
					"验证数据库Currency");
			
			List<OrderStatusLogPo> results = orderShardingWapper.selectOrderStatusesLogByOrderId(orderId);
			
			Logger.verifyEquals(2, results.size(), "验证数据库OrderStatusLog数量");
			
			for(OrderStatusLogPo result : results) {
				
				if (result.getOrderStatus() == 2 && confirmOfflinePayReq.getCsId() != null)
					Logger.verifyEquals(String.format("客服【 %s】确认线下收款", confirmOfflinePayReq.getCsId()),
							result.getMemo(), "验证数据库memo");
				else if (result.getOrderStatus() == 2)
					Logger.verifyEquals(String.format("确认线下收款"),
							result.getMemo(), "验证数据库memo");
				else if (result.getOrderStatus() == 17 && confirmOfflinePayReq.getCsId() != null)
					Logger.verifyEquals(
							String.format("客服【 %s】接单",
									confirmOfflinePayReq.getCsId()),
							result.getMemo(), "验证数据库memo");
				else
					Logger.verifyEquals(String.format("接单"),
							result.getMemo(), "验证数据库memo");
			}
			
			Logger.verifyEquals(paymentId, ymtOrders.getPaymentId(),
					"验证数据库PaymentId");
			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal.ZERO,
					"验证数据库ThirdpartyPaidAmount");
			DataUtil.verifyDecimalEquals(BigDecimal.ZERO, BigDecimal.ZERO,
					"验证数据库AccountPaidAmount");

			if (needPayAmount.compareTo(realNeedPayAmount) != 0)
				DataUtil.verifyDecimalEquals(
						needPayAmount.add(orderDiscount),
						realNeedPayAmount, "验证数据库realNeedPayAmount");
			
			if (ymtOrders.getPickupInStores()) {
				verifyPickupInfo(orderId, 0, ymtOrders.getGoodsOwnerId());
			}
		}
	}

	/**
	 * 支付表(payment)
	 * 
	 * @param expectedPayment
	 * @param actualPayment
	 */
	private void verifyPaymentDb(PaymentPo expectedPayment,
			PaymentPo actualPayment) {

		Logger.comment(String.format("验证支付表(payment) ,paymentId :%s",
				expectedPayment.getPaymentId()));

		DataUtil.verifyDecimalEquals(expectedPayment.getThirdpartyAmount(),
				actualPayment.getThirdpartyAmount(), "验证数据库ThirdpartyAmount");
		DataUtil.verifyDecimalEquals(expectedPayment.getRealPaidAmount(),
				actualPayment.getRealPaidAmount(), "验证数据库RealPaidAmount");
		
		Logger.verifyEquals(expectedPayment.getTradeType(),
				actualPayment.getTradeType(), "验证数据库TradeType");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualPayment.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualPayment.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		DataUtil.verifyDecimalEquals(expectedPayment.getPayableAmount(),
				actualPayment.getPayableAmount(), "验证数据库PayableAmount");
		Logger.verifyEquals(expectedPayment.getPaymentId(),
				actualPayment.getPaymentId(), "验证数据库PaymentId");
		Logger.verifyEquals(expectedPayment.getUserId(),
				actualPayment.getUserId(), "验证数据库UserId");
		DataUtil.verifyDecimalEquals(expectedPayment.getThirdpartyDiscount(),
				actualPayment.getThirdpartyDiscount(),
				"验证数据库ThirdpartyDiscount");

		DataUtil.verifyDecimalEquals(expectedPayment.getPayDeductAmount(),
				actualPayment.getPayDeductAmount(), "验证数据库payDeductAmount");

		Logger.verifyEquals(expectedPayment.getPayStatus(),
				actualPayment.getPayStatus(), "验证数据库PayStatus");

		Logger.verifyEquals(expectedPayment.getRetryTimes(),
				actualPayment.getRetryTimes(), "验证数据库RetryTimes");

		DataUtil.verifyDecimalEquals(expectedPayment.getAccountAmount(),
				actualPayment.getAccountAmount(), "验证数据库AccountAmount");
		Logger.verifyEquals(expectedPayment.getPayType(),
				actualPayment.getPayType(), "验证数据库PayType");

		Logger.verifyEquals(expectedPayment.getMemo(), actualPayment.getMemo(),
				"验证数据库Memo");

		Logger.verifyEquals(expectedPayment.getSettleCurrency(),
				actualPayment.getSettleCurrency(), "验证数据库SettleCurrency");
		Logger.verifyEquals(expectedPayment.getSettleRate(),
				actualPayment.getSettleRate(), "验证数据库SettleRate");
		Logger.verifyEquals(expectedPayment.getForeignAmount(),
				actualPayment.getForeignAmount(), "验证数据库ForeignAmount");
		Logger.verifyEquals(expectedPayment.getRealForeignAmount(),
				actualPayment.getRealForeignAmount(), "验证数据库RealForeignAmount");
		Logger.verifyEquals(expectedPayment.getPayCurrency(),
				actualPayment.getPayCurrency(), "验证数据库PayCurrency");
		Logger.verifyEquals(expectedPayment.getExchangeRate(),
				actualPayment.getExchangeRate(), "验证数据库ExchangeRate");
		Logger.verifyEquals(expectedPayment.getShenqiPaymentRmbAmount(),
				actualPayment.getShenqiPaymentRmbAmount(), "验证数据库ShenqiPaymentRmbAmount");
		Logger.verifyEquals(expectedPayment.getAppName(),
				actualPayment.getAppName(), "验证数据库appName");
		Logger.verifyEquals(expectedPayment.getGatewayPayType(),
				actualPayment.getGatewayPayType(), "验证数据库gatewayPayType");
	}
	
	public static void verifyPickupInfo(long orderId, Integer pickupType,
			Long goodsOwnerId) {

		Logger.comment("验证自提信息");

		PickInStorePo pickInStorePo = orderShardingWapper.selectPickInStorebyOrderId(
				orderId).get(0);

		Logger.verifyEquals(pickupType == 0 ? (byte) 2 : (byte) 0,
				pickInStorePo.getPickupStatus(), "验证PickupStatus");

		OrderWapper orderWapper = new OrderWapper();

		PickUpCodePo pickUpCodePo = orderWapper
				.selectPickUpCodeByOrderId(orderId);

		Logger.verifyNotNull(pickUpCodePo.getPickupCode(), "验证PickupCode不为空");
		Logger.verifyEquals(goodsOwnerId, pickUpCodePo.getGoodsOwnerId(),
				"验证GoodsOwnerId");

		Logger.verifyEquals(pickInStorePo.getPickupCode(),
				pickUpCodePo.getPickupCode(), "验证PickupCode");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_001() {
		Logger.start(true, "创建一个线下支付订单,使用折扣，外币支付，货头确认收款成功");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false, true);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.TEN, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "200");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			confirmOfflinePayReq.setCsId(1234L);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			verifyDb(userId, resp.getTradingId(), resp.getPayableAmount(),
					resp.getOrderList());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_002() {
		Logger.start(true, "创建一个线下支付订单，日币支付，货头确认收款成功, 不传入csId");
		try {
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createJPProxychain(500058147, 1, false, true);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, 500058147,
					productBean.getCurrency(), true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "200");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(500058147L);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			verifyDb(userId, resp.getTradingId(), resp.getPayableAmount(),
					resp.getOrderList());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_003() {
		Logger.start(true, "创建一个线下支付订单，货头确认收款成功, 不传入csId");
		try {
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "200");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			verifyDb(userId, resp.getTradingId(), resp.getPayableAmount(),
					resp.getOrderList());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_004() {
		Logger.start(true, "创建一个线下支付订单，其他买手确认收款失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			long sellerId = resp.getOrderList().get(0).getSellerId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "200");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(sellerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			Logger.verifyEquals(false, confirmOfflinePayCall.getSuccess(), "验证success");
			Logger.verifyEquals(201, confirmOfflinePayCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("用户%s不是订单%s的所有者", sellerId, orderId), confirmOfflinePayCall.getMsg(), "验证msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_005() {
		Logger.start(true, "创建一个线下支付订单，买家未确认线下支付，买手确认收单失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			Logger.verifyEquals(false, confirmOfflinePayCall.getSuccess(), "验证success");
			Logger.verifyEquals(201, confirmOfflinePayCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("买家未确认线下支付"), confirmOfflinePayCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_006() {
		Logger.start(true, "创建一个线下支付订单，买家已经线上支付，买手确认收单失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, autotest_goodsOwnerId,
					productBean.getCurrency(), true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "6000");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			Logger.verifyEquals(false, confirmOfflinePayCall.getSuccess(), "验证success");
			Logger.verifyEquals(201, confirmOfflinePayCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("买家未确认线下支付"), confirmOfflinePayCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_007() {
		Logger.start(true, "创建一个非线下支付订单，买家已经线上支付，买手确认收单失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService.placeOrder(
					productBean, BigDecimal.ZERO, autotest_goodsOwnerId,
					productBean.getCurrency(), false);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId());
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			Logger.verifyEquals(false, confirmOfflinePayCall.getSuccess(), "验证success");
			Logger.verifyEquals(201, confirmOfflinePayCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("订单不支持线下支付"), confirmOfflinePayCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_008() {
		Logger.start(true, "创建一个预订订单，买手确认收单失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingBookingOrderCallService
					.placeBookingOrder(productBean);
			
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			Logger.verifyEquals(false, confirmOfflinePayCall.getSuccess(), "验证success");
			Logger.verifyEquals(201, confirmOfflinePayCall.getCode(), "验证code");
			Logger.verifyEquals(String.format("订单不支持线下支付"), confirmOfflinePayCall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmOfflinePay_009() {
		Logger.start(true, "创建一个线下支付自提订单，货头确认收款成功");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0, true);
			
			long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			
			SqTradingCallService.payOrder(userId, resp.getTradingId(), "wxsqBuyer", "200");
			
			Logger.comment("前置数据准备结束");
			
			confirmOfflinePayReq.setOrderId(orderId);
			confirmOfflinePayReq.setSellerId(autotest_goodsOwnerId);
			confirmOfflinePayReq.setCsId(1234L);
			
			confirmOfflinePayCall.setData(confirmOfflinePayReq);
			confirmOfflinePayCall.callService();

			verifyDb(userId, resp.getTradingId(), resp.getPayableAmount(),
					resp.getOrderList());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
